package com.zprmb.udzt;

import java.text.DecimalFormat;
import java.util.Date;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.TimeUtils;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;

/**
 * 转入
 * 
 * @author shenshao
 */

public class DexterityPayInActivity extends BaseActivity implements
		OnClickListener {

	private ImageView iv_v4_dexterity_in_back;
	private TextView tv_v4_dexterity_in_title;
	private int increaseNum;// 增量
	private TextView tv_v4_dexterity_pay_in_keyong;
	private TextView tv_v4_dexterity_pay_in_chiyou;
	private TextView tv_v4_dexterity_pay_in_chiyouup;
	private TextView tv_v4_dexterity_pay_in_kezhuanru;
	private EditText et_v4_dexterity_pay_in_minAmount;
	private TextView tv_v4_dexterity_pay_in_shengyu;
	private TextView tv_v4_dexterity_pay_in_daotime;
	private TextView tv_v4_dexterity_pay_in_updata;
	private TextView tv_shouyi;
	private String[] str = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
	private int remainAmount;
	private Double million;
	private ImageView img_nowork;
	private ScrollView sv;
	private int minAmount;
	
	private RelativeLayout rl_back;
	DecimalFormat df = new DecimalFormat("###0.00");
	DecimalFormat df2 = new DecimalFormat("###");
	private String productId;
	private double inAmount, availableAmount;// 可转入金额

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v4_dexterity_pay_in_activity);
		BbtApplication.getInstance().addActvity(this);

		initView();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}

	protected void onResume() {
		super.onResume();

		payIn();
	}

	private void initView() {
		iv_v4_dexterity_in_back = (ImageView) findViewById(R.id.img_back);
		iv_v4_dexterity_in_back.setOnClickListener(this);
		img_nowork = (ImageView) findViewById(R.id.iv_v4_nonetwork);
		tv_v4_dexterity_in_title = (TextView) findViewById(R.id.tv_v4_dexterity_in_title);
		tv_shouyi = (TextView) findViewById(R.id.tv_shouyi);
		sv = (ScrollView) findViewById(R.id.sv);
		tv_v4_dexterity_in_title.setText("智投宝转入");
		million = getIntent().getDoubleExtra("million", 0);
		img_nowork.setOnClickListener(this);
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		tv_v4_dexterity_pay_in_keyong = (TextView) findViewById(R.id.tv_v4_dexterity_pay_in_keyong);
		tv_v4_dexterity_pay_in_chiyou = (TextView) findViewById(R.id.tv_v4_dexterity_pay_in_chiyou);
		tv_v4_dexterity_pay_in_chiyouup = (TextView) findViewById(R.id.tv_v4_dexterity_pay_in_chiyouup);
		tv_v4_dexterity_pay_in_kezhuanru = (TextView) findViewById(R.id.tv_v4_dexterity_pay_in_kezhuanru);
		et_v4_dexterity_pay_in_minAmount = (EditText) findViewById(R.id.et_v4_dexterity_pay_in_minAmount);
		tv_v4_dexterity_pay_in_shengyu = (TextView) findViewById(R.id.tv_v4_dexterity_pay_in_shengyu);
		tv_v4_dexterity_pay_in_daotime = (TextView) findViewById(R.id.tv_v4_dexterity_pay_in_daotime);
		tv_v4_dexterity_pay_in_updata = (TextView) findViewById(R.id.tv_v4_dexterity_pay_in_updata);
		tv_v4_dexterity_pay_in_updata.setOnClickListener(this);
		tv_shouyi
				.setText("1.每日收益=(用户持有的转入金额/10000)X每日 万 元收益。3000举例：您持有金额20000元，当日的每日万元收益为"
						+ million + "元，代入计算公式，您的当日收益为：" + million * 2 + "元.");
		et_v4_dexterity_pay_in_minAmount.setText(getIntent().getStringExtra(
				"money"));
		sv.setVisibility(View.GONE);
		rl_back.setOnClickListener(this);
		et_v4_dexterity_pay_in_minAmount
				.addTextChangedListener(new TextWatcher() {

					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
						// TODO Auto-generated method stub

					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
						// TODO Auto-generated method stub

					}

					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						if (!s.toString().equals("")
								&& availableAmount < Integer.valueOf(s
										.toString())) {
							tv_v4_dexterity_pay_in_updata.setText("余额不足,马上去充值");

						} else {
							tv_v4_dexterity_pay_in_updata.setText("转入");
						}
					}
				});
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		case R.id.tv_v4_dexterity_pay_in_updata:

			verityUser();
			break;
		case R.id.iv_v4_nonetwork:
			payIn();
			break;
		case R.id.rl_back:
			finish();
			break;
		}
	}

	private void verityUser() {
		VolleryUtil vollery = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success") == 1) {
					JSONObject json = jsonObject.optJSONObject("data");
					Intent intent = null;
					if (!json.optBoolean("nameAuth")
							&& !json.optBoolean("isBindCard")) {
						intent = new Intent(DexterityPayInActivity.this,
								RealName.class);
						startActivity(intent);
						return;
					}
					if (json.optString("bankMobileStatus") != null
							&& !json.optString("bankMobileStatus").equals("")
							&& json.optString("bankMobileStatus").equals(
									"WAIT_CHECK")) {
						intent = new Intent(DexterityPayInActivity.this,
								BankCardActivity.class);
						intent.putExtra("bankcard", json.optString("cardNo"));
						intent.putExtra("isbank", 1);
						intent.putExtra("logourl", json.optString("logoUrl"));
						intent.putExtra("banknameEn",
								json.optString("bankNameEn"));
						intent.putExtra("banknameCn",
								json.optString("bankNameCn"));
						intent.putExtra("status",
								json.optString("bankMobileStatus"));
						startActivity(intent);
						return;
					}
					if (!json.optBoolean("isBindCard")) {
						intent = new Intent(DexterityPayInActivity.this,
								BankCardActivity.class);
						startActivity(intent);
						return;
					}
					if (!json.optBoolean("payPass")) {
						intent = new Intent(DexterityPayInActivity.this,
								Set_tr_pw.class);
						startActivity(intent);
						return;
					}
					if (tv_v4_dexterity_pay_in_updata.getText().toString()
							.equals("余额不足,马上去充值")) {

						Intent intent5 = new Intent(
								DexterityPayInActivity.this,
								V4AddMoneySActiviy.class);
						startActivity(intent5);
						return;
					}
					intent = new Intent(DexterityPayInActivity.this,
							PayDexterityActivity.class);
					intent.putExtra("type", "IN");

					if (et_v4_dexterity_pay_in_minAmount.getText().toString()
							.equals("")) {
						ToastUtil.showCenterToast(DexterityPayInActivity.this,
								"请输入转入金额");
						return;
					}
					if (Integer.valueOf(et_v4_dexterity_pay_in_minAmount
							.getText().toString()) < minAmount) {
						ToastUtil.showCenterToast(DexterityPayInActivity.this,
								"转入金额" + minAmount + "元起");
						return;
					}
					if ((Integer.valueOf(et_v4_dexterity_pay_in_minAmount
							.getText().toString()) > inAmount)
							&& Integer.valueOf(et_v4_dexterity_pay_in_minAmount
									.getText().toString()) < remainAmount) {
						ToastUtil.showCenterToast(DexterityPayInActivity.this,
								"转入金额超出持有上限");
						return;
					}
					if ((Integer.valueOf(et_v4_dexterity_pay_in_minAmount
							.getText().toString()) > remainAmount)) {
						ToastUtil.showCenterToast(DexterityPayInActivity.this,
								"项目剩余金额不足");
						return;
					}
					if ((Integer.valueOf(et_v4_dexterity_pay_in_minAmount
							.getText().toString()) - minAmount) % increaseNum != 0) {
						ToastUtil.showCenterToast(DexterityPayInActivity.this,
								"转入金额的增量要为" + increaseNum + "的倍数");
						return;
					}
					tv_v4_dexterity_pay_in_updata
							.setBackgroundResource(R.drawable.btn_unclick);
					tv_v4_dexterity_pay_in_updata.setEnabled(false);
					tv_v4_dexterity_pay_in_updata
							.setBackgroundResource(R.drawable.my_button);
					tv_v4_dexterity_pay_in_updata.setEnabled(true);
					intent.putExtra("amount", et_v4_dexterity_pay_in_minAmount
							.getText().toString());
					intent.putExtra("productId", productId);
					Log.e("智投宝id", productId);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_in_from_bottom,
							R.anim.slide_out_to_bottom);
				} else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(DexterityPayInActivity.this,
								LoginActivity.class);
						startActivity(intent);
					} else {
						ToastUtil.showCenterToast(DexterityPayInActivity.this,
								jsonObject.optString("comment"));
					}
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		vollery.setHeaders("token", getToken());
		vollery.setHeaders("client", "android");
		vollery.setShow(true);
		vollery.setRequestMethod(0);
		vollery.execute(DexterityPayInActivity.this, Constant.URL
				+ "/api/v5/user/userInfo", "");
	}

	private void payIn() {
		VolleryUtil vollery = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				LogUtils.e("转入", jsonObject.toString());

				if (jsonObject.optInt("success") == 1) {
					sv.setVisibility(View.VISIBLE);
					img_nowork.setVisibility(View.GONE);
					JSONObject json = jsonObject.optJSONObject("data");
					tv_v4_dexterity_pay_in_keyong.setText(df.format(Double
							.parseDouble(json.optString("availableAmount")))
							+ "元");
					tv_v4_dexterity_pay_in_chiyou.setText(df.format(Double
							.parseDouble(json.optString("currentAmount")))
							+ "元");
					tv_v4_dexterity_pay_in_chiyouup.setText("(转入上限"
							+ df2.format(Double.parseDouble(json
									.optString("maxAmount")) / 10000) + "万元)");
					tv_v4_dexterity_pay_in_kezhuanru.setText(df.format(Double
							.parseDouble(json.optString("inAmount"))));
					tv_v4_dexterity_pay_in_shengyu.setText(df2.format(Double
							.parseDouble(json.optString("remainAmount"))));
					remainAmount = json.optInt("remainAmount");
					increaseNum = json.optInt("increaseNum");
					minAmount = json.optInt("minAmount");
					String time = TimeUtils.formatTime(new Date(Long
							.valueOf(json.optString("countTime"))));
					Log.e("时间", time);
					tv_v4_dexterity_pay_in_daotime.setText(time + "("
							+ str[json.optInt("dayOfWeek") - 1] + ")");
					productId = json.optString("productId");
					inAmount = json.optDouble("inAmount");
					availableAmount = json.optDouble("availableAmount");
					if (!et_v4_dexterity_pay_in_minAmount.getText().toString()
							.equals("")
							&& availableAmount < Integer
									.valueOf(et_v4_dexterity_pay_in_minAmount
											.getText().toString())) {
						tv_v4_dexterity_pay_in_updata.setText("余额不足,马上去充值");

					}

				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub
				sv.setVisibility(View.GONE);
				img_nowork.setVisibility(View.VISIBLE);
				Toast.makeText(DexterityPayInActivity.this, "请求失败   请检查网络设置",
						2000).show();
			}
		};
		vollery.setShow(true);
		vollery.setHeaders("token", getToken());
		vollery.setHeaders("client", "android");
		vollery.setRequestMethod(1);
		vollery.execute(this, Constant.URL + "api/v2/claim/fund/current/in", "");
	}
}
